<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Plugin_Slide_Type_Interface
 * @category   Interface
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */
abstract class Bbce_Slidetype_Abstract
{        
    /**
     * Title of the plugin, as shown in the interface
     * 
     * @var string
     */
    protected $_title = '';
    
    /**
     * Description of the plugin, as shown in the interface
     * 
     * @var string
     */
    protected $_description = '';
        
    /**
     * Zend_Db_Table object that contains the data for
     * each slide type slide entity.
     * 
     * @var Zend_Db_Table
     */
    private $_data = null;
    
    /**
     * Builds the slide requested.  Returns an associative
     * array of key => value pairs of variables to be assigned
     * to the view object.
     * 
     * Example:  return array('url' => 'http://www.google.com');
     * 
     * This will assign the value of "http://www.google.com" to the view
     * variable "url".  In the view, you will access this by "echo $this->url";
     * 
     * @param int $slideId
     * @return array
     */
    abstract public function buildSlide($slideId);
    
    /**
     * Generates a thumbnail for the given slide.
     * 
     * @param $slideId
     * @return PHPGD image object
     */
    abstract public function generateSlideThumbnail($slideId);    
    
    /**
     * Generates a Zend_Form object which is displayed when
     * editing the given slide type.  The $values array is
     * a key => value associative array that maps the form
     * field names with their default values, if any.
     * 
     * @param array $values
     * @return object Zend_Form
     */
    abstract public function getConfigurationForm($values = array());
    
    /**
     * Creates a new instance of the Slidetype Data object to 
     * save slide data.
     */
    public function __construct()
    {
        $this->_data = new Slidetype_Data();
    }
    
    /**
     * Gets the title of the plugin
     * 
     * @return string
     */
    public function getTitle()
    {
        return $this->_title;
    }
    
    /**
     * Gets the description of the plugin
     * 
     * @return string
     */
    public function getDescription()
    {
        return $this->_description;
    }

    /**
     * Saves the configuration form for the given slide.  Returns
     * an array of error messages, if any.
     * 
     * @param int $slideId
     * @param array $data
     * @return array
     */
    public function saveConfigurationForm($slideId, array $data)
    {
        $form = $this->getConfigurationForm(array('slideId' => $slideId));
        
        if ($form->isValid($data)) {
            $existingData = $this->find($slideId);

            $dataStore = array(
                'slideId' => $slideId,
                'data'    => serialize($data),
            );
            
            if (is_null($existingData)) {
                $this->_data->insert($dataStore);
            } else {
                $this->_data->update($dataStore, null);
            }
        }
        
        return $form->getErrorMessages();        
    }
    
    public function find($slideId)
    {
        $result = $this->_data->find($slideId);

        if (!is_null($result)) {
            $data = unserialize($result->data);
            
            return array_merge(array('slideId' => $slideId), $data);
        }
        
        return null;
    }
}